﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.KhaoThi
{
    public partial class UcKiemTraBangDiemQuaTrinhHocTap : QldtSdh.GUI.Common.BaseUserControl, Common.IHocKyChangable
    {
        bool allowUpdate = false;

        public DAL.Models.HocKy HocKy { get; private set; }

        public void SetHocKy(DAL.Models.HocKy newHocKy)
        {
            this.HocKy = newHocKy;
            LoadDonVi();
        }

        public void ChangeHocKy(DAL.Models.HocKy newHocKy)
        {
            // if (newHocKy != this.HocKy)
            this.SetHocKy(newHocKy);
        }

        public UcKiemTraBangDiemQuaTrinhHocTap()
        {
            InitializeComponent();
            gridHocVien.AddRowIndexing();
            gridLopHocPhan.AddRowIndexing();
        }

        void LoadDonVi()
        {
            allowUpdate = false;
            cbxDonVi.DataSource = BizDonViDaoTao.GetList();
            cbxDonVi.DisplayMember = "TenDonVi";
            cbxDonVi.ValueMember = "MaDonViGUID";
            LoadLopHocPhan();
        }

        void LoadLopHocPhan()
        {
            gridLopHocPhan.AutoGenerateColumns = false;
            var donVi = cbxDonVi.SelectedItem as DonVi;
            bdLopHocPhan.DataSource = null;
            if (HocKy != null && donVi != null)
            {
                var ls = BizLopHocPhan.GetList(HocKy.MaHocKyGUID, donVi.MaDonViGUID);
                bdLopHocPhan.DataSource = ls;
            }
            gridLopHocPhan.DataSource = bdLopHocPhan;
            LoadThongTinChiTiet();
            LoadThongTinHocVien();
        }

        void LoadThongTinChiTiet()
        {
            var lopHP = bdLopHocPhan.Current as LopHocPhan;
            if (lopHP != null)
            {
                var hocPhan = BizHocPhan.GetByID(lopHP.MaHocPhanGUID);
                if (hocPhan != null)
                    txtTenLopHocPhan.Text = string.Format("[{0}]-{1} ({2} TC)", hocPhan.MaHocPhan, hocPhan.TenHocPhan, hocPhan.SoTinChi);
                else
                    txtTenLopHocPhan.Text = "";
                var lsGVGD = BizPhanCongGiangDay.GetList(lopHP.MaLopHocPhanGUID);
                string tenGV = "";
                if (lsGVGD != null && lsGVGD.Count > 0)
                {
                    for (int i = 0; i < lsGVGD.Count; i++)
                        if (i == (lsGVGD.Count - 1))
                            tenGV += string.Format("{0}", lsGVGD[i]._TenDayDu);
                        else
                            tenGV += string.Format("{0}, ", lsGVGD[i]._TenDayDu);
                }
                txtGiaoVienPhuTrach.Text = tenGV;
            }
            else
            {
                txtGiaoVienPhuTrach.Text = "";
                txtTenLopHocPhan.Text = "";
            }
        }

        void LoadThongTinHocVien()
        {
            var lopHP = bdLopHocPhan.Current as LopHocPhan;
            bdHocVien.DataSource = null;
            if (lopHP != null)
            {
                gridHocVien.AutoGenerateColumns = false;
                var ls = BizLopHocPhan_HocVien.GetList(lopHP.MaLopHocPhanGUID);
                bdHocVien.DataSource = ls;
                if (lopHP.TrangThaiBangDiemQTHT == (int)TRANGTHAI_BANGDIEM.DANHAPXONG || lopHP.TrangThaiBangDiemQTHT == (int)TRANGTHAI_BANGDIEM.DAPHEDUYET)
                    btnBaoLoi.Enabled = btnXacNhan.Enabled = true;
                else
                    btnBaoLoi.Enabled = btnXacNhan.Enabled = false;
            }
            else
                btnBaoLoi.Enabled = btnXacNhan.Enabled = false;
            gridHocVien.DataSource = bdHocVien;
            allowUpdate = true;
        }

        private void cbxDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadLopHocPhan();
        }

        private void bdLopHocPhan_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadThongTinChiTiet();
                LoadThongTinHocVien();
            }
        }

        private void gridHocVien_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == colDiemThi.Index)
            {
                decimal temp = 0;
                if (!decimal.TryParse(e.FormattedValue.ToString(), out temp))
                {
                    DialogUtils.ShowError("Bạn phải nhập số");
                    e.Cancel = true;
                }
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (UpdateBangDiemQTHT(TRANGTHAI_BANGDIEM.DAPHEDUYET) == CODE_RESULT_RETURN.ThanhCong)
            {
                DialogUtils.ShowMessage("Đã xác nhận bảng điểm QTHT thành công");
                LoadLopHocPhan();
            }
            else
            {
                DialogUtils.ShowMessage("Lỗi xác nhận bảng điểm QTHT");
            }
        }


        private void btnBaoLoi_Click(object sender, EventArgs e)
        {
            if (UpdateBangDiemQTHT(TRANGTHAI_BANGDIEM.BAOLOINHAP) == CODE_RESULT_RETURN.ThanhCong)
            {
                DialogUtils.ShowMessage("Đã báo lỗi bảng điểm QTHT thành công");
                LoadLopHocPhan();
            }
            else
            {
                DialogUtils.ShowMessage("Lỗi bão lỗi bảng điểm QTHT");
            }
        }

        CODE_RESULT_RETURN UpdateBangDiemQTHT(TRANGTHAI_BANGDIEM trangThaiBangDiem)
        {
            var lsHV = bdHocVien.DataSource as List<HocVien>;
            var lopHP = bdLopHocPhan.Current as LopHocPhan;
            if (lsHV != null && lsHV.Count > 0 && lopHP != null)
                return BizLopHocPhan.UpdateBangDiem(lopHP.MaLopHocPhanGUID, trangThaiBangDiem, FormMain.NguoiSuDung.UserName);
            return CODE_RESULT_RETURN.ThatBai;
        }
    }
}
